package cn.com.pxto.service.impl;

import java.util.Iterator;
import java.util.List;

import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.DicDAO;
import cn.com.pxto.dao.UserDAO;
import cn.com.pxto.dao.UserObjectRuleDAO;
import cn.com.pxto.model.Dic;
import cn.com.pxto.model.User;
import cn.com.pxto.model.UserObjectRule;
import cn.com.pxto.service.UserManager;

public class UserManagerImpl implements UserManager {
	private UserDAO userDAO=null;
	private UserObjectRuleDAO userObjectRuleDAO=null;
	private DicDAO dicDAO=null;
	@SuppressWarnings("unchecked")
	@Override
	public void createUser(User user) {
		userDAO.saveUser(user);
		//create UserObjectRule
		int type=user.getType().intValue();
		//TODO type=3 
		if( type == 3){
			type=2;
		}
		String value = "00"+type+"___";//第一层权限
		SearchParam[] params = {new SearchParam("type","visit_func",SearchParam.SEARCH_PARAM_TYPE_NUMBER),
								new SearchParam("defValue",value,SearchParam.SEARCH_PARAM_TYPE_STRING_HAFEINDISTINCT)};
		List dicList = dicDAO.listDic(params);
		Iterator iter = dicList.iterator();
		while(iter.hasNext()){
			Dic dic = (Dic)iter.next();
			value = dic.getDefValue()+"___";//第二层权限
			SearchParam[] param = {new SearchParam("type","visit_func",SearchParam.SEARCH_PARAM_TYPE_NUMBER),
					new SearchParam("defValue",value,SearchParam.SEARCH_PARAM_TYPE_STRING_HAFEINDISTINCT)};
			List subDicList = dicDAO.listDic(param);
			if (subDicList!=null && subDicList.size()>0) {
				for (int i = 0; i < subDicList.size(); i++) {
					Dic subDic =(Dic)subDicList.get(i);
					UserObjectRule subUor=new UserObjectRule(new Long(user.getUid().toString()),new Long(subDic.getId().toString()));
					userObjectRuleDAO.saveRule(subUor);
				}
			}
			UserObjectRule uor=new UserObjectRule(new Long(user.getUid().toString()),new Long(dic.getId().toString()));
			userObjectRuleDAO.saveRule(uor);
		}
	}

	@Override
	public void deleteUserById(int id) {
//		TODO guest info about user
		userDAO.deleteUser(new Integer(id));
//		delete userObjectRule
		userObjectRuleDAO.deleteRulesBySrc(id);
	}

	@Override
	public List<User> findUserByParams(Object[] obj, PageInfo pageInfo) {
		return null;
	}
	/**
	 * return null if user not exist
	 */
	@Override
	public User getUser(String userName) {
		return userDAO.getUser(userName);
	}

	@Override
	public List<User> getUserByDeptId(Integer deptId, PageInfo pageInfo) {
		return null;
	}

	@Override
	public User getUserById(int id) {
		return userDAO.getUser(new Integer(id));
	}

	@Override
	public boolean isUserExist(String userName) {
		User user=userDAO.getUser(userName);
		if(user == null){
			return false;
		}else
			return true;
	}

	@Override
	public List<User> listAllUser(PageInfo pageInfo) {
		return userDAO.listUser(pageInfo);
	}

	@Override
	public void update(User user) {
		userDAO.updateUser(user);
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setUserObjectRuleDAO(UserObjectRuleDAO userObjectRuleDAO) {
		this.userObjectRuleDAO = userObjectRuleDAO;
	}

	public void setDicDAO(DicDAO dicDAO) {
		this.dicDAO = dicDAO;
	}

}
